package com.fhp.fourth;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.zone.ZoneRules;
import java.util.Set;

public class TelephoneBilling {


    // 计算通话时长（分钟）
    public static long calculateTimeSpan(ZonedDateTime startTime, ZonedDateTime endTime) {
        Duration duration = Duration.between(startTime, endTime);
        long minutes = duration.toMinutes(); // 获取总分钟数
        if (duration.getSeconds() % 60 > 0) { // 如果有剩余秒数，向上进位到下一分钟
            minutes++;
        }
        return minutes;
    }

    // 计算通话费用
    public static BigDecimal calculateFee(long timeLength) {
        BigDecimal fee;
        if (timeLength <= 20) {
            fee = BigDecimal.valueOf(timeLength * 0.05); // 每分钟收费0.05美元
        } else {
            fee = BigDecimal.valueOf(20 * 0.05); // 前20分钟收费
            fee = fee.add(BigDecimal.valueOf((timeLength - 20) * 0.10)); // 超过20分钟部分每分钟收费0.10美元
        }
        // 不足1分钟按1分钟计算
        if (fee.compareTo(BigDecimal.ONE) < 0) {
            fee = BigDecimal.ONE;
        }
        return fee;
    }

    // 判断是否夏令时
    public static boolean isDaylightTime(ZonedDateTime dateTime, ZoneId zoneId) {
        return zoneId.getRules().isDaylightSavings(dateTime.toInstant());
    }

    public static void main(String[] args) {
        // 示例用法
        ZonedDateTime startTime = ZonedDateTime.of(2024, 3, 23, 12, 0, 0, 0, ZoneId.systemDefault());
        ZonedDateTime endTime = startTime.plusMinutes(25); // 25分钟后挂断
        long timeLength = calculateTimeSpan(startTime, endTime);
        BigDecimal fee = calculateFee(timeLength);
        System.out.println("通话时长：" + timeLength + "分钟");
        System.out.println("通话费用：" + fee + "美元");
    }


}
